<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Template Online Editor
 *
 * @category	Admin
 */
class ThemeAction extends AdminAction
{


	//初始化
	public function _initialize()
	{
		parent::_initialize();
	}


	//模板列表
	public function editTemplate()
	{
		import('ORG.Util.Input');

		$theme = isset($_REQUEST['theme']) ? $_REQUEST['theme'] : 'Default';
		$tplFile = isset($_REQUEST['tpl_file']) ? base64_decode($_REQUEST['tpl_file']) : '';
		$tplContent = '';

		if ($this->isPost())
		{
		    if (!C('ACP_EDIT_TEMPLATE_ENABLE'))
    		{
    			$this->error(L('theme.tpl_editor_disabled'));
    		}
			if (!is_writable($tplFile))
			{
			    $message = sprintf(L('file_not_writable'), $tplFile);
				$this->error($message);
			}

			$tplContent = Input::getVar($_POST['tpl_content']);
			file_put_contents($tplFile, $tplContent);

			$jumpUrl = __ACTION__.'/theme/'.$theme.'/tpl_file/'.base64_encode($tplFile);
			$this->assign('jumpUrl', $jumpUrl);
			$this->success(L('theme.tpl_edited_succeed'));
		}

		/*------------ 获取所有主题 ------------*/
		$tpl = dir(TMPL_PATH);
		while (false !== ($entry = $tpl->read()))
		{
			if (is_dir(TMPL_PATH . $entry) && !in_array($entry, array('.','..')))
			{
				$themes[$entry] = $entry;
			}
		}
		$tpl->close();
		$this->assign('themeOptions', $themes);
		$this->assign('themeSelected', $theme);

		/*------------ 获取主题下的模板文件 -------------*/
		$path = TMPL_PATH . $theme .'/Home/';
		$fp = dir($path);
		while (false !== ($entry = $fp->read()))
		{
		    if (is_file($path . $entry))
		    {
		    	$files[] = $entry;
		    }
		}
		$fp->close();

		if (!empty($files))
		{
			sort($files);
			foreach ($files as $value)
			{
			    $tmp = explode('_', $value);
		        $templates[$tmp[0]][] = array('name' => $value, 'path' => $path . $value);
			}
		}

		if ($tplFile)
		{
			C('TOKEN_ON', false); //禁止令牌标示（否则会多出令牌标示）
			$tplContent = file_get_contents($tplFile);
			$tplContent = Input::forTarea($tplContent);
		}
		$this->assign('tplFile', $tplFile);
		$this->assign('tplContent', $tplContent);
		$this->assign('templates', $templates);
		$this->display();
	}

}
// End Class

/* End of file ThemesAction.class.php */
/* Location: App/Lib/Action/Admin/ThemesAction.class.php */